﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CmpClass;

namespace CmpEdu
{
    public partial class FormTest4_3 : Form
    {
        public FormTest4_3()
        {
            InitializeComponent();
        }

        private void FormTest4_3_Load(object sender, EventArgs e)
        {
            this.Text = "三角函数计算器";
            tbAngle.Text = "30";
            rBtnSin.Checked = true; //这个很重要，不然后面的BtnCalc_Click就要添加一层判断：一个rBtn都没有选中
            labelResult.Text = "三角函数值是：";
        }

        /// <summary>
        /// btn_click事件，计算三角函数值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCalc_Click(object sender, EventArgs e)
        {
            if(tbAngle.Text=="")
            {
                MessageBox.Show("未输入进行计算的角度", "文本框为空", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //下面是计算四个角度值
            if(rBtnSin.Checked) //计算sin函数
            {
                SinFunc sinVar = new SinFunc();
                sinVar.Val = double.Parse(tbAngle.Text); //把文本框的结果赋值给属性Angle                
                labelResult.Text = "sin" + tbAngle.Text + "°= " + sinVar.GetResult().ToString("F4");
            }
            else if (rBtnCos.Checked) //计算cos函数
            {
                CosFunc cosVar = new CosFunc()
                {
                    Angle = double.Parse(tbAngle.Text)
                };
                labelResult.Text = "cos" + tbAngle.Text + "°= " + cosVar.GetResult().ToString("F4");
            }
            else if (rBtnTan.Checked) //计算tan函数
            {
                TanFunc tanVar = new TanFunc()
                {
                    Angle = double.Parse(tbAngle.Text)
                };
                labelResult.Text = "tan" + tbAngle.Text + "°= " + tanVar.GetResult().ToString("F4");
            }
            else //计算cot函数
            {
                CotFunc cotVar = new CotFunc()
                {
                    Angle = double.Parse(tbAngle.Text)
                };
                labelResult.Text = "cot" + tbAngle.Text + "°= " + cotVar.GetResult().ToString("F4");
            }
        }

        /* 写错啦，不能用可以keydown
        /// <summary>
        /// 文本框的限制，只允许输入数字和back，enter键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TbAngle_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Back)
            {
                if (tbAngle.Text!="")
                {
                    tbAngle.Text = tbAngle.Text.Remove(tbAngle.TextLength - 1);
                }
            }
            else if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
            {
                SendKeys.Send("{TAB}"); //如果输入了enter，则焦点调到计算按钮上
            }
            else if (!(e.KeyValue >= 48 && e.KeyValue <= 57 || e.KeyValue >= 96 && e.KeyValue <= 105))
            {
                if (tbAngle.Text == "")
                {
                    tbAngle.Text = "";

                }
                else
                {
                    tbAngle.Text = tbAngle.Text.Remove(tbAngle.TextLength-1);
                }
            }
        }
        */

        /// <summary>
        /// 不允许输入其他的字符，可以是数字/enter/back/tab
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TbAngle_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back)
            {
                if (tbAngle.Text != "")
                {
                    tbAngle.Text = tbAngle.Text.Remove(tbAngle.TextLength - 1);
                }
            }
            else if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            {
                //SendKeys.Send("{TAB}"); //如果输入了enter，则焦点调到计算按钮上
                BtnCalc.Focus();
                //第一种方法tb和btn的TabIndex必须是连着的
            }
            //不是数字/tab/enter/back，则清除掉
            else if (!(e.KeyValue >= 48 && e.KeyValue <= 57 || e.KeyValue >= 96 && e.KeyValue <= 105))
            {
                if (tbAngle.Text!="")
                {
                    tbAngle.Text = tbAngle.Text.Remove(tbAngle.TextLength - 1);
                }
                //else
                //{
                //    tbAngle.Text = "";
                //}                
            }
        }
    }
}
